package com.lesso.lpq.sunflower.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.lpq.sunflower.core.ApiResponse;
import com.lesso.lpq.sunflower.pojo.dto.DeviceDTO;
import com.lesso.lpq.sunflower.pojo.dto.DeviceQueryDTO;
import com.lesso.lpq.sunflower.pojo.vo.DeviceVO;
import com.lesso.lpq.sunflower.service.IDeviceService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @ClassName : DeviceController
 * @Description :
 * @Author : 罗培棋
 * @Date: 2025-07-05 08:48
 */
@RestController
@RequestMapping("/base/device")
public class DeviceController {
    @DubboReference
    private IDeviceService deviceService;

    @PostMapping
    public ApiResponse<Long> save(@Valid @RequestBody DeviceDTO dto) {
        return ApiResponse.ok("success", deviceService.save(dto));
    }

    @GetMapping("{deviceCode}")
    public ApiResponse<DeviceVO> getByDeviceCode(@PathVariable String deviceCode) {
        return ApiResponse.ok("SUCCESS", deviceService.getByDeviceCode(deviceCode));
    }

    @GetMapping("/page")
    public ApiResponse<Page<DeviceVO>> page(DeviceQueryDTO dto) {
        return ApiResponse.ok("success", deviceService.page(dto));
    }

    @GetMapping("/custom/page")
    public ApiResponse<Page<DeviceVO>> customPage(DeviceQueryDTO dto) {
        return ApiResponse.ok("success", deviceService.customPage(dto));
    }

    /*    @PutMapping("{deviceCode}")
        public ApiResponse<Object> updateByIdCard(@PathVariable String deviceCode){
            deviceService.updateByIdCard(deviceCode);
            return ApiResponse.ok();
        }*/
    @DeleteMapping
    public ApiResponse<Object> delete(@RequestParam("id") Long id) {
        Assert.notNull(id, "id不能为空");
        if (id < 0) {
            throw new RuntimeException("id不能小于0");
        }
        deviceService.deleteById(id);
        return ApiResponse.ok();
    }
}
